package com.ruoyi.counter.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.counter.domain.Tools;
import com.ruoyi.counter.mapper.ToolsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.counter.domain.Countersub;
import com.ruoyi.counter.mapper.CounterMapper;
import com.ruoyi.counter.domain.Counter;
import com.ruoyi.counter.service.ICounterService;

/**
 * 工器具柜档案Service业务层处理
 *
 * @author xll
 * @date 2021-07-27
 */
@Service
public class CounterServiceImpl implements ICounterService
{
    @Autowired(required = false)
    private CounterMapper counterMapper;

    @Autowired(required = false)
    private ToolsMapper toolsMapper;

    /**
     * 查询工器具柜档案
     *
     * @param counterid 工器具柜档案ID
     * @return 工器具柜档案
     */
    @Override
    public Counter selectCounterById(Long counterid)
    {
        return counterMapper.selectCounterById(counterid);
    }

    /**
     * 查询工器具柜档案列表
     *
     * @param counter 工器具柜档案
     * @return 工器具柜档案
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Counter> selectCounterList(Counter counter)
    {
        return counterMapper.selectCounterList(counter);
    }

    /**
     * 新增工器具柜档案
     *
     * @param counter 工器具柜档案
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCounter(Counter counter)
    {

        counter.setDeptId(counter.getOrganid());
        int rows = counterMapper.insertCounter(counter);
        insertCountersub(counter);
        return rows;
    }

    /**
     * 修改工器具柜档案
     *
     * @param counter 工器具柜档案
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCounter(Counter counter)
    {
        counterMapper.deleteCountersubByCounterid(counter.getCounterid());
        insertCountersub(counter);
        return counterMapper.updateCounter(counter);
    }

    /**
     * 批量删除工器具柜档案
     *
     * @param counterids 需要删除的工器具柜档案ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCounterByIds(Long[] counterids)
    {
        counterMapper.deleteCountersubByCounterids(counterids);
        return counterMapper.deleteCounterByIds(counterids);
    }

    /**
     * 删除工器具柜档案信息
     *
     * @param counterid 工器具柜档案ID
     * @return 结果
     */
    @Override
    public int deleteCounterById(Long counterid)
    {
        counterMapper.deleteCountersubByCounterid(counterid);
        return counterMapper.deleteCounterById(counterid);
    }

    @Override
    public List<Tools> selectToolsList(Long counterid) {

        return toolsMapper.selectToolsListByCounterId(counterid);
    }

    /**
     * 新增工器具副柜信息
     *
     * @param counter 工器具柜档案对象
     */
    public void insertCountersub(Counter counter)
    {
        List<Countersub> countersubList = counter.getCountersubList();
        Long counterid = counter.getCounterid();
        if (StringUtils.isNotNull(countersubList))
        {
            List<Countersub> list = new ArrayList<Countersub>();
            for (Countersub countersub : countersubList)
            {
                countersub.setCounterid(counterid);
                list.add(countersub);
            }
            if (list.size() > 0)
            {
                counterMapper.batchCountersub(list);
            }
        }
    }
}
